package com.lzw.cpvideoplayer.activity;

import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.google.android.material.navigation.NavigationView;
import com.lzw.cpvideoplayer.R;
import com.lzw.cpvideoplayer.adapter.HomePagerAdapter;
import com.lzw.cpvideoplayer.data.HeadData;
import com.lzw.cpvideoplayer.mvp.presenter.VideoPresenter;
import com.lzw.cpvideoplayer.mvp.view.BaseActivity;
import com.lzw.cpvideoplayer.mvp.view.IBaseView;
import com.lzw.cpvideoplayer.ulit.MyViewPager;
import com.lzw.cpvideoplayer.ulit.Permission;
import com.lzw.cpvideoplayer.ulit.Ulit;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.core.app.ActivityCompat;
import androidx.viewpager.widget.ViewPager;

public class MainActivity extends BaseActivity<IBaseView, VideoPresenter> implements IBaseView<HeadData>,MyViewPager.onClickTouchListener, View.OnClickListener {
    /**
     * 主页部分
     */
    private  HomePagerAdapter adapter;//
    private Permission mPermission;
    private ImageView upImage;
    private EditText editText;
    private ImageView Userimage;
    private NavigationView nav_View;
    private boolean mIsTouch = false;
    private ImageButton btn;
    private MyViewPager mVp;
    private CardView btnVideo;//视频
    private CardView btnMusic;//音乐
    private CardView btnCollect;//收藏
    private CardView btnDownLoad;//下载

    private final int LoopMiss = 3000;
    private static final String TAG = "MainActivity";
    private Handler handler = new Handler();
    private static List<Integer> sList = new ArrayList<>();
    static {//定义轮播图图片
        sList.add(R.mipmap.pic1);
        sList.add(R.mipmap.pic2);
        sList.add(R.mipmap.pic3);
        sList.add(R.mipmap.pic4);
        sList.add(R.mipmap.pic5);
        sList.add(R.mipmap.pic6);
        sList.add(R.mipmap.pic7);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        int i = ActivityCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS);
        Log.e(TAG, "initView: "+i);
        //ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.RECEIVE_SMS,Manifest.permission.READ_SMS},100);
        //初始化控件
        editText = findViewById(R.id.edit_url);//顶部搜索框
        btn = findViewById(R.id.put_btn);//搜索按钮
//        upImage = findViewById(R.id.admin);//更新日志按钮
        mVp = findViewById(R.id.vp_home);//viewPager对象
         adapter = new HomePagerAdapter();//适配器
        btnVideo = findViewById(R.id.main_btn_video);
        btnDownLoad = findViewById(R.id.main_btn_download);
        btnMusic = findViewById(R.id.main_btn_music);
        btnCollect = findViewById(R.id.main_btn_collect);

        nav_View = findViewById(R.id.nav_view);//NavigationView对象
        View v = nav_View.getHeaderView(0);//获得头布局
        Userimage = v.findViewById(R.id.imageView);//获得用户头像
        Ulit.toRoundBitmap(this,Userimage,R.drawable.wx);//改变头像形状
        mVp.setAdapter(adapter);//轮播图适配器
        adapter.setData(sList);//为轮播图适配器赋值数据
        //adapter.setCurrentItem(100,false);

    //绑定点击事件
        btnCollect.setOnClickListener(this);
        btnMusic.setOnClickListener(this);
        btnDownLoad.setOnClickListener(this);
        btnVideo.setOnClickListener(this);

        mVp.setCurrentItem(adapter.getPicSizeCount()*100);//跳转到100个后开始轮播
        mVp.setOnClickTouchListener(MainActivity.this);//点击

        btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {//点击事件
                if(event.getAction() == MotionEvent.ACTION_DOWN){//按下切换图片
                    btn.setImageResource(R.drawable.cat);//显示猫猫图
                }else if(event.getAction() == MotionEvent.ACTION_UP){
                    btn.setImageResource(R.drawable.search_low);
                }
                return false;
            }
        });

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(editText.getText().toString().equals("")){

                    AlertDialog.Builder build = new AlertDialog.Builder(MainActivity.this,R.style.edit_dolog);

                    build.setNegativeButton("关闭", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {

                                }
                            })
                            .setView(R.layout.dlog_view)
                            .show();

                    return;
                }
                //跳转到搜索结果页
                Intent intent = new Intent(MainActivity.this,SearchResultActivity.class);
                intent.putExtra("search_text",editText.getText().toString());//将搜索框内的内容发送给搜索内容页
                startActivity(intent);
            }
        });
//
//        upImage.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
////                AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
////                alert.setTitle("更新日志")
////                        .setView(R.layout.update_text)
////                        .show();
//            }
//        });
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        //当我这个界面绑定到窗口的时候
        handler.post(looperRun);
        }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        //解除绑定
        handler.removeCallbacks(looperRun);
    }
    Runnable looperRun = new Runnable() {
        @Override
        public void run() {

            if(!mIsTouch){
               
                int currentItem = mVp.getCurrentItem();
                mVp.setCurrentItem(++currentItem);

            }
            handler.postDelayed(looperRun,LoopMiss);//延迟1秒


        }
    };
    @Override
    public VideoPresenter createPresenter() {
        return new VideoPresenter();
    }

    @Override
    public void ShowViewData(HeadData data) {
        //LoadDiaLog.getInstance(MainActivity.this).onLoadDismiss();
    }


    @Override
    public void onPagerTouch(boolean isTouch) {
        Log.e(TAG, "onPagerTouch: "+isTouch );
        mIsTouch = isTouch;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode == mPermission.REQUEST_CODE){
            for (int i = 0; i < grantResults.length; i++) {
                //permissions权限名 grantResults用户是否同意 -1拒绝 0同意
                if(grantResults[i]!= PackageManager.PERMISSION_GRANTED){
                    Log.e(TAG, "用户拒绝了: "+permissions[i]);
                    Log.e(TAG, "用户拒绝了: "+grantResults[i]);
                }else {
                    Log.e(TAG, "用户同意了: "+permissions[i]);
                    Log.e(TAG, "用户同意了: "+grantResults[i]);
                }

            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.main_btn_video:
                Intent intent = new Intent(MainActivity.this,VideoMainActivity.class);
                startActivity(intent);
                break;
            case R.id.main_btn_music:break;
            case R.id.main_btn_collect:break;
            case R.id.main_btn_download:break;
        }
    }
}